$(document).ready(function(){
	//绑定返回顶部按钮
	BC.returnTop('#toTop',300,100);
	
	var $subMenu = $('.sub-menu'),
		$uBTab = $('#u-b-tab'),
		$bookPicWrap = $('.book-pic-wrap'),
		//还书
		$dialogBox = $('#book-info-dialog-box'),
		$sendTipsForm = $('#send-tips-form'),
		$receiver = $('#receiver'),
		isBind = false;
	$subMenu.find('a').click(function(){
		var li = $uBTab.children('li');
		li.each(function(){
			if($(this).hasClass('active')){
				$(this).removeClass('active');
			}
		});
	});
	$uBTab.children('.no-sub').find('a').click(function(){
		var sub = $subMenu.children('li');
		sub.each(function(){
			if($(this).hasClass('active')){
				$(this).removeClass('active');
			}
		});
	});
	//图片hover出现操作项
	$bookPicWrap.hover(function(){
		$(this).children('.control').fadeTo(300,0.7);
	},function(){
		$(this).children('.control').fadeTo(300,0);
	});
	
	$('div.book-info').on('click','.check-pass',function(e){	//审核通过操作
		var $me = $(this),	
			$parent = $me.closest('.book-info'),
			bid = $me.attr('data-bid'),
			pic = $me.attr('data-pic'),
			$pass = $('#user-book-pass'),	//通过审核图书的div
			$uBTitle = $pass.children('.u-b-title'),		//title
			$noBook = $pass.children('.no-book'),	//没有图书的提示信息
			$control = $me.closest('.control'),
			isCheck = 1;
		if(confirm('确定审核通过？')){
			BC.ajax({
				type:'GET',
				url:'/bookcrossing/book/changeIsCheck',
				data:{bid:bid,ischeck:isCheck},
				s:function(){
					$parent.fadeOut(function(){
						if($noBook.length>0){
							$noBook.remove();
						}
						$uBTitle.after($parent);
						$control.html(
							'<a href="javascript:void(0)" class="check-unpass" title="不通过" data-bid="'+bid+'"><apan class="glyphicon glyphicon-ban-circle"></apan></a>'
							+'<a href="javascript:void(0)" class="del" title="删除" data-bid="'+bid+'"  data-pic="'+pic+'"><apan class="glyphicon glyphicon-trash"></apan></a>'
						);
						$parent.fadeIn(0);
					});
				},
				f:function(){
					alert('操作失败');
				},
				e:function(){
					
				}
			});
		}
		
	}).on('click','.check-unpass',function(){	//审核不通过操作
		var $me = $(this),	
			$parent = $me.closest('.book-info'),
			bid = $me.attr('data-bid'),
			pic = $me.attr('data-pic'),
			$unpass = $('#user-book-unpass'),	//通过审核图书的div
			$uBTitle = $unpass.children('.u-b-title'),		//title
			$noBook = $unpass.children('.no-book'),	//没有图书的提示信息
			$control = $me.closest('.control'),
			isCheck = 2;
		if(confirm('确定审核不通过？')){
			BC.ajax({
				type:'GET',
				url:'/bookcrossing/book/changeIsCheck',
				data:{bid:bid,ischeck:isCheck},
				s:function(){
					$parent.fadeOut(function(){
						if($noBook.length>0){
							$noBook.remove();
						}
						$uBTitle.after($parent);
						$control.html(
							'<a href="javascript:void(0)" class="check-pass" title="通过" data-bid="'+bid+'"><apan class="glyphicon glyphicon-ok"></apan></a>'
						);
						$parent.fadeIn(0);
					});
				},
				f:function(){
					alert('操作失败');
				},
				e:function(){
					
				}
			});
		}
	}).on('click','.del',function(){	//删除操作
		var $me = $(this),	
			$parent = $me.closest('.book-info'),
			bid = $me.attr('data-bid'),
			pic = $me.attr('data-pic');
		if(confirm('确定删除该图书？')){
			BC.ajax({
				type:'GET',
				url:'/bookcrossing/book/del',
				data:{bid:bid,picurl:pic},
				s:function(){
					$parent.fadeOut(function(){
						$parent.remove();
					});
				},
				f:function(resp){
					alert(resp.msg);
				}
			});
		}
	}).on('click','.return',function(){	//还书操作
		var $me = $(this),
			bid = $me.attr('data-bid'),
			belong = $me.attr('data-belong');
		$receiver.val(belong);
		$dialogBox.modal({
			backdrop:'static',
			keyboard:false
		});
		if(!isBind){
			$sendTipsForm.submit(function(e){
				e.preventDefault();
				BC.ajax({
					type:'POST',
					url:'/bookcrossing/sendTips',
					data:$sendTipsForm.serialize()+'&bid='+bid+'&type=1',
					s:function(resp){
						$dialogBox.one('hidden.bs.modal',function(e){
							alert(resp.msg);
						});
						$dialogBox.modal('hide');
					},
					f:function(resp){
						alert(resp.msg);
					}
				});
			});
			isBind = true;
		}
	});
});

